package com.yq.ylmall.activity.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yq.ylmall.activity.mapper.ActivityRuleMapper;
import com.yq.ylmall.activity.mapper.ActivitySkuMapper;
import com.yq.ylmall.client.product.ProductFeignClient;
import com.yq.ylmall.dto.activity.ActivityInfoDto;
import com.yq.ylmall.model.activity.ActivityInfo;
import com.yq.ylmall.activity.mapper.ActivityInfoMapper;
import com.yq.ylmall.activity.service.IActivityInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yq.ylmall.model.activity.ActivityRule;
import com.yq.ylmall.model.activity.ActivitySku;
import com.yq.ylmall.vo.activity.ActivityInfoVo;
import com.yq.ylmall.vo.product.ProductInfoVo;
import com.yq.ylmall.vo.product.ProductPageVo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 活动表 服务实现类
 * </p>
 *
 * @author cherry
 * @since 2024-06-07
 */
@Slf4j
@Service
public class ActivityInfoServiceImpl extends ServiceImpl<ActivityInfoMapper, ActivityInfo> implements IActivityInfoService {
    @Autowired
    private ActivityRuleMapper activityRuleMapper;
    @Autowired
    private ActivitySkuMapper activitySkuMapper;
    @Autowired
    private ProductFeignClient productFeignClient;

    @Override
    public IPage<ActivityInfoVo> queryActivityInfo(ActivityInfoDto activityInfoDto) {
        IPage<ActivityInfoVo> page = new Page<>(activityInfoDto.getPageNum(), activityInfoDto.getPageSize());
        return baseMapper.queryActivityPage(page,activityInfoDto);
    }

    /**
     * 根据活动id获取活动规则数据
     * @param id
     * @return
     */
    @Override
    public Map<String, Object> findActivityRuleList(Long id) {
        Map<String, Object> map = new HashMap<>();
        //根据活动id查询，查询规则列表 activity_rule 表
        LambdaQueryWrapper<ActivityRule> wrapperActivityRule = new LambdaQueryWrapper<>();
        wrapperActivityRule.eq(ActivityRule::getActivityId,id);
        List<ActivityRule> activityRuleList = activityRuleMapper.selectList(wrapperActivityRule);
        map.put("activityRuleList",activityRuleList);
        //根据活动id查询。查询使用规则商品skuId列表 activity_sku 表
        List<ActivitySku> activitySkuList = activitySkuMapper.selectList(
                new LambdaQueryWrapper<ActivitySku>()
                        .eq(ActivitySku::getActivityId, id)
        );
        //获取所有skuId
        List<Long> skuIdList = activitySkuList.stream().map(ActivitySku::getSkuId).collect(Collectors.toList());
        //通过远程调用 ylmall-service-product 模块接口，根据skuId列表 得到商品信息
        List<ProductInfoVo> productInfoVoList = skuIdList.stream().map(skuId -> productFeignClient.getById(skuId)).collect(Collectors.toList());
        map.put("productInfoVoList",productInfoVoList);
        return map;
    }
}
